<?php

namespace app\api\controller;
use think\facade\Request;
use app\common\controller\Api;
use think\facade\Db;
use think\facade\Queue;
use think\facade\Cache;

class BJEquity extends Api
{
    /**
     * 不需要鉴权方法
     * index、save不需要鉴权
     * ['index','save']
     * 所有方法都不需要鉴权
     * [*]
     */
    protected $noAuth = ['*'];
    protected $noNeedLogin = ['*'];
    /*
        北京移动业务
    */
    //生产地址
    //protected $url = 'https://139.hl139.cn/chinamobiles/zt/interFace/forword?';
    //测试地址
    // protected $publicKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCF0cj2FY1yK/7MKmtF2SCgKhoKF4I5BFoZOl5hmEaUaeFrnfugqZVcApzgi/vRjbi9O4Qvk/y+sv2pyrSytN1QFJS2NlvfswZsaktPHsnAMxuzvLpszAnyfW/RWz00TJcJAyzwpAON+DxTBQRac6bbWg1JXJNIZfcUD5Q88bmhLQIDAQAB';
    // protected $appid = '20240112105459000001';
    protected $secret = '5918084B0196CA3D0A45298F3740BCF264F0887067C187F003A7F4B2FEE25E00';
    protected $chanCode = '20240112105459000001';
    // protected $strings = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    //北京移动权益发放接口
    public function grant()
    {
        $params = $this->request->param();
        if (!isset($params['body'])) {
            $res['resultCode']    = '1001';
            $res['resultMsg']     = '请求body不能为空';
            $res['transactionId'] = '';
            $res['body']          = '';
            return json_encode($res, JSON_UNESCAPED_UNICODE);
        }
        $params['body'] = html_entity_decode($params['body']);
        $params['body'] = str_replace('&quot;', '"', $params['body']);
        // dump($params['body']);
        $sign = $this->getSign($params);
        // halt($params,$sign,$params['sign']);
        //判断签名是否一致
        if ($sign != $params['sign']) {
            $res['resultCode']    = '30034';
            $res['resultMsg']     = '签名错误';
            $res['transactionId'] = $params['transactionId'];
            $res['body']          = '';
            return json_encode($res, JSON_UNESCAPED_UNICODE);
        }

        //一致的话就保存数据
        $body = \json_decode($params['body'], true);
        // dump($body);die;
        $data['content']        = json_encode($params, JSON_UNESCAPED_UNICODE);
        $data['transaction_id'] = $params['transactionId'];
        $data['mobile']         = $body['phoneNo'];
        $data['is_renew']       = $body['isRenew'];
        $data['ext_order_id']   = $body['extOrderId'];
        $data['order_type']     = $body['orderType'];
        $data['prod_code']      = $body['prodCode'];
        $data['count']          = isset($body['count']) ? $body['count'] : '';
        $data['count_type']     = isset($body['count_type']) ? $body['count_type'] : '';
        $data['addtime']        = time();
        $jobHandlerName         = 'app\api\controller\Queue';
        $jobQueueName           = 'bjyd';
//        var_export($data);exit;
        // Queue::push($jobHandlerName, ['name'=>"5255"], $jobQueueName);
        Queue::later(1, $jobHandlerName, $data, $jobQueueName);
        //添加成功
        $res['resultCode']          = '0';
        $res['resultMsg']           = '成功';
        $res['transactionId']       = $params['transactionId'];
        $res['body']['orderStatus'] = '00S';
        $res['body']['orderDesc']   = '开通成功';
        return json_encode($res, JSON_UNESCAPED_UNICODE);


    }

    //作用：生成签名
    public function getSign($Parameters)
    {
        //abilityCode=DISTRIBUTOR_SSO_LOGIN&accessToken=fcd0abf455114b7ab978d272a15adbd4&body={"receviNo":"15188889999","channelCode":"OUTERXQYD_A001","secretKey":"oZIujN\/gcnTtGkc9aU9bPsyjF38rxhqyyygAGhO\/P10="}&randomstr=Vw5YNjIe&timestamp=20240117114051&transactionId=202401121054590000012024011711405100000001
        //abilityCode=DISTRIBUTOR_SSO_LOGIN&accessToken=fcd0abf455114b7ab978d272a15adbd4&body={"receviNo":"15010097824","channelCode":"OUTERXQYD_A001","secretKey":"0Lmmb+Ps\/thdpNUmcz6pR5Xg1kNmxubRtCwKRJ53gs8="}&randomstr=buhz0Awk&timestamp=20240117114128&transactionId=202401121054590000012024011711412800000001
        // 第一步：按参数名的ASCII码从小到大排序
        ksort($Parameters);
        // 拼接参数名和参数值
        $stringA = "";
        foreach ($Parameters as $key => $value) {
            if ($value !== "" && $key !== "sign") {
                $stringA .= $key . "=" . $value . "&";
            }
        }
        // dump($stringA);
        $stringA = rtrim($stringA, "&"); // 去除末尾的&符号

        // dump($stringA);
        // $stringA = str_replace("+", "%2B", $stringA);
        // dump($stringA);
        return hash('sha256', $stringA);
        // dump($stringA);
        // 第二步：对stringA进行SHA256运算
        // $secretKey = base64_decode($key);
        // $sign = hash_hmac('md5', $stringA, $secretKey, true);
        // dump(base64_encode($sign));
        // // 输出签名值
        // return base64_encode($sign);
    }
}
